package cn.shop.admin.controller;

import cn.shop.admin.service.OrderService;
import cn.shop.base.MyPage;
import cn.shop.transcation.order.entity.Order;
import cn.shop.transcation.order.entity.OrderQueryReq;
import cn.shop.utils.ApiResult;
import cn.shop.utils.enums.OrderStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * 订单管理
 *
 * @author jzw
 * @since 2021/12/23 10:15
 */
@RestController
@RequestMapping("/api/v1/order")
public class OrderController {

	@Resource
	private OrderService orderService;

	/**
	 * 按条件获取订单列表
	 *
	 * @param query 查询条件
	 * @return 返回结果
	 */
	@GetMapping("/queryByPage")
	public ApiResult<MyPage<Order>> list(MyPage<Order> page, @Validated OrderQueryReq query) {
		// if (Utils.isEmpty(query, "state")) {
		// 	LocalDateTime now = LocalDateTime.now();
		// 	query.setStartTime(now.withHour(0).withMinute(0));
		// 	query.setEndTime(now.withHour(23).withMinute(59));
		// }
		return ApiResult.success(orderService.orderList(page, query));
	}

	/**
	 * 按条件统计订单数量和金额
	 *
	 * @param query 查询条件
	 * @return 返回结果
	 */
	@GetMapping("/statistics")
	public ApiResult<Map<String, Object>> statistics(@Validated OrderQueryReq query) {
		// if (Utils.isEmpty(query,"state")) {
		// 	LocalDateTime now = LocalDateTime.now();
		// 	query.setStartTime(now.withHour(0).withMinute(0));
		// 	query.setEndTime(now.withHour(23).withMinute(59));
		// }
		return ApiResult.success(orderService.statistics(query));
	}

	/**
	 * 退回订单
	 *
	 * @param ids 编号
	 * @return 返回结果
	 */
	@PutMapping("/giveBack")
	public ApiResult<Map<String, Object>> giveBack(@Validated @RequestBody @NotEmpty(message = "请传递订单id") List<Long> ids) {
		return ApiResult.result(orderService.giveBack(ids));
	}

	/**
	 * 冻结订单
	 *
	 * @param ids 编号
	 * @return 返回结果
	 */
	@PutMapping("/frozen")
	public ApiResult<Map<String, Object>> frozen(@Validated @RequestBody @NotEmpty(message = "请传递订单id") List<Long> ids) {
		return ApiResult.result(orderService.changeStatus(ids, OrderStatus.FROZEN));
	}

	/**
	 * 解冻订单
	 *
	 * @param ids
	 * @return
	 */
	@PutMapping("/unfrozen")
	public ApiResult<Map<String, Object>> unfrozen(@Validated @RequestBody @NotEmpty(message = "请传递订单id") List<Long> ids) {
		return ApiResult.result(orderService.changeStatus(ids, OrderStatus.PASSED));
	}

}
